\defmodule {QQPlot}

This class implements \emph{QQ-plot} (or quantile-quantile plot)
objects that compare two probability distributions.
The data is given as a list of $x$-coordinates $(x_1, x_2, \ldots, x_{n})$,
and one is given a reference continuous probability distribution  $F(x)$.
One first sorts the $x_i$ in ascending order, then noted $x_{(i)}$, and
plots the points $(F^{-1}(p_i), x_{(i)})$, where
$i= 1, 2, \ldots, n$ and  $p_i = (i- 1/2)/n$,
to see if the data $x_i$ comes from the reference distribution  $F(x)$.
The graph of the straight line $y=x$ is also plotted for comparison.



\bigskip\hrule
\begin{code}
\begin{hide}
/*
 * Class:        QQPlot
 * Description:  qq-plot
 * Environment:  Java
 * Software:     SSJ 
 * Copyright (C) 2001  Pierre L'Ecuyer and Universite de Montreal
 * Organization: DIRO, Universite de Montreal
 * @author       Richard Simard
 * @since        May 2011

 * SSJ is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License (GPL) as published by the
 * Free Software Foundation, either version 3 of the License, or
 * any later version.

 * SSJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * A copy of the GNU General Public License is available at
   <a href="http://www.gnu.org/licenses">GPL licence site</a>.
 */
\end{hide}
package umontreal.iro.lecuyer.charts;
   import umontreal.iro.lecuyer.probdist.ContinuousDistribution;\begin{hide}
   import java.util.Arrays;
\end{hide}


public class QQPlot extends XYLineChart \begin{hide} {
   private double[][] Q;        // data points
   private double[][] Lin;      // line y = x

   private void initLinear (double a, double b)
   {
      // line y = x in [a, b] by steps of h
      int m = 100;
      double h = (b - a)/ m;
      Lin = new double[2][m+1];
      for (int i = 0; i <= m; i++)
         Lin[0][i] = Lin[1][i] = a + h * i;
   }


   private void initPoints (ContinuousDistribution dist, double[] data,
                            int numPoints)
   {
      int i;
      double p;
      Q = new double[2][numPoints];     // q_i = cdf^(-1)(p_i)

      for (i = 0; i < numPoints; i++)
         Q[1][i] = data[i];
      Arrays.sort(Q[1]);
      for (i = 0; i < numPoints; i++) {
         p = (i + 0.5)/numPoints;
         Q[0][i] = dist.inverseF(p);
      }
   }\end{hide}
\end{code}

%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection*{Constructors}

\begin{code}

   public QQPlot (String title, String XLabel, String YLabel,
                  ContinuousDistribution dist, double[] X) \begin{hide} {
      this (title, XLabel, YLabel, dist, X, X.length);
   }\end{hide}
\end{code}
\begin{tabb}
   Constructs a new \texttt{QQPlot} instance using the points \texttt{X}.
   \texttt{title} is a title, \texttt{XLabel} is a short description of
   the $x$-axis, and \texttt{YLabel}  a short description of the $y$-axis.
   The plot is a QQ-plot of the points
   $(F^{-1}(p_i), x_{(i)})$, $i= 1, 2, \ldots, n$, where  $p_i = (i- 1/2)/n$,
   $x_i = $\texttt{ X[$i$-1]}, $x_{(i)}$ are the  sorted points,
    and $x = F^{-1}(p) =
   $\texttt{ dist.inverseF($p$)}. The points \texttt{X} are not  sorted.
\end{tabb}
\begin{htmlonly}
   \param{title}{chart title.}
   \param{XLabel}{Label on $x$-axis.}
   \param{YLabel}{Label on $y$-axis.}
   \param{dist}{Reference distribution}
   \param{X}{points.}
\end{htmlonly}
\begin{code}

   public QQPlot (String title, String XLabel, String YLabel,
                  ContinuousDistribution dist, double[] X, int numPoints) \begin{hide} {
      super();
      initPoints (dist, X, numPoints);
      initLinear (Q[1][0], Q[1][numPoints-1]);
      dataset = new XYListSeriesCollection(Q, Lin);
      // --- dashed line for y = x
      ((XYListSeriesCollection)dataset).setDashPattern(1, "dashed");
      init (title, XLabel, YLabel);
   }\end{hide}
\end{code}
\begin{tabb}
   Similar to the constructor %
  \method{QQPlot}{String,String,String,ContinuousDistribution,double[]}%
  \texttt{(title, XLabel, YLabel, dist, X)} %
   above, except that only \emph{the first} \texttt{numPoints} of \texttt{X}
    are plotted.
\end{tabb}
\begin{htmlonly}
   \param{title}{chart title.}
   \param{XLabel}{Label on $x$-axis.}
   \param{YLabel}{Label on $y$-axis.}
   \param{dist}{Reference distribution}
   \param{X}{point set.}
   \param{numPoints}{number of points to plot}
\end{htmlonly}
\begin{code}

   public QQPlot (String title, String XLabel, String YLabel,
                  ContinuousDistribution dist, double[][] data, int r) \begin{hide} {
      this (title, XLabel, YLabel, dist, data[r], data[r].length);
   }
}\end{hide}
\end{code}
\begin{tabb}
   Constructs a new \texttt{QQPlot} instance.
   \texttt{title} is a title, \texttt{XLabel} is a short description of
   the $x$-axis, and \texttt{YLabel}  a short description of the $y$-axis.
   The input vectors in \texttt{data} represents several sets of $x$-points.
   $r$ determine the set of points to be plotted in the QQ-plot, that is,
   one will plot only the points \texttt{data[r][i]},
   for $i=0, 1, \ldots, (n-1)$ and a given $r$, where $n$ is the number
   of points in set $r$. The points are assumed to follow the distribution
  \texttt{dist}.
\end{tabb}
\begin{htmlonly}
   \param{title}{chart title.}
   \param{XLabel}{Label on $x$-axis.}
   \param{YLabel}{Label on $y$-axis.}
   \param{dist}{Reference distribution}
   \param{data}{series of point sets.}
   \param{r}{set of points to plot}
\end{htmlonly}
